//
//  SegmentCornerView.m
//  SegmentCornerView
//
//  Created by 宋飞龙 on 16/4/8.
//  Copyright © 2016年 宋飞龙. All rights reserved.
//

#import "SegmentCornerView.h"
#import "UIButton+Corners.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface SegmentCornerView () {
    UIView * lineView;
}

@end

@implementation SegmentCornerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

-(void)awakeFromNib {
//    [self lineView];
}

- (void)setTitleDatas:(NSArray *)titleDatas  selectedIndex:(NSInteger)index{
    _titleDatas = titleDatas;
    [self makeButtonView:titleDatas selectedIndex:index];
    [self lineViewWithIndex:index];

}

- (void) lineViewWithIndex:(NSInteger)index {
    UIButton * button = (UIButton *)[self viewWithTag:0];
    UIView * centerLineView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH/2, 0, 1, button.frame.size.height)];
    centerLineView.backgroundColor = [UIColor colorWithHexString:@"01c8fc"];
    [self addSubview:centerLineView];
    lineView = [[UIView alloc] initWithFrame:CGRectMake((WIDTH/2)*index, button.frame.size.height-3, WIDTH/2+1, 3)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"01c8fc"];
    [self addSubview:lineView];
}

- (void)makeButtonView:(NSArray *)titleDatas selectedIndex:(NSInteger)index {
    for (int i = 0; i < 2; i++) {
        UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.frame = CGRectMake((WIDTH/2)*i, 0, WIDTH/2, self.frame.size.height);
        if (i == (int)index) {
            [button setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
            button.selected = YES;
        } else {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.selected = NO;
        }
        [button setTitle:titleDatas[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(blockIndex:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.tag = i+1;
        [self addSubview:button];
        
    }
}

- (void)setTextDatas:(NSArray *)textArr{
    for (UIView * subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton * button = (UIButton *)subView;
            for (UIButton * btn in button.subviews) {
                if ([btn isKindOfClass:[UILabel class]]) {
                    UILabel * label = (UILabel*)btn;
                    if (label.tag == 10 || label.tag == 11) {
                        label.hidden = YES;
                        [label removeFromSuperview];
                    }
                }
            }
        }
    }
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    NSDictionary *dict1 = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
    for (int i = 0; i<textArr.count; i++) {
        CGRect frame = [self.titleDatas[i] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading   attributes:dict context:nil];
        CGRect cornerFrame = [textArr[i] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 50) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading   attributes:dict1 context:nil];
        [self makeLabelView:i titleFrame:frame cornerFrame:cornerFrame text:textArr[i]];
    }
}

- (void)makeLabelView:(int)i titleFrame:(CGRect)titleFrame cornerFrame:(CGRect)cornerFrame text:(NSString *)text{
    UIButton * button = (UIButton *)[self viewWithTag:i+1];
    CGFloat labelX = (button.frame.size.width-titleFrame.size.width)/2 + titleFrame.size.width;
    [button addSubview:[button setCornerView:i titleFrame:titleFrame cornerFrame:cornerFrame text:text buttonTitieX:labelX]];
}

- (void)blockIndex:(UIButton *)button {
    if (self.indexChangeBlock) {
        self.indexChangeBlock(button.tag-1);
    }
    for (UIView  * view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton * btn  = (UIButton *)view;
            btn.selected = NO;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    button.selected = YES;
    [button setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self->lineView.frame = CGRectMake((WIDTH/2)*(button.tag-1), button.frame.size.height-3, WIDTH/2+1, 3);
    } completion:^(BOOL finished) {
        
    }];
}

@end
